import { HomeCardProps, HomeCardsProps } from "@/views/home/components/cards/types.ts";
import { useRenderIcon } from "@/components/ReIcon/src/hooks.ts";
import { useCopyToClipboard } from "@pureadmin/utils";
import { message, notification } from "ant-design-vue";
import { invoke } from "@tauri-apps/api/tauri";

import { EsIndexInfo } from "@/views/home/types.ts";
export function useCards(props: HomeCardsProps) {
	const esIndexInfos = toRef(props, "esIndexInfos");
	const cardWidth = toRef(props, "cardWidth");

	return {
		esIndexInfos,
		cardWidth,
	};
}

export function useCard(props: HomeCardProps, emit: any) {
	const cardWidth = toRef(props, "cardWidth");
	const esIndexInfoTemp = toRef(props, "esIndexInfo");
	const esIndexInfo = ref<EsIndexInfo>();
	const updateIndexInfoStatus = ref<number>(0);
	const openDrawer = ref<boolean>(false);
	const checked = ref<boolean>(false);
	const { copied, update } = useCopyToClipboard();

	const statusCircleInner = h(useRenderIcon("fluent-mdl2:status-circle-inner"));
	const statusCircleFill = h(useRenderIcon("mingcute:heart-fill"));

	const showDrawer = () => {
		openDrawer.value = true;
	};
	const handleDrawerClose = () => {
		openDrawer.value = false;
	};

	const updateIndexInfo = () => {
		updateIndexInfoStatus.value = updateIndexInfoStatus.value + 1;
	};
	const handleCopy = (value: string) => {
		update(value);
		if (copied.value) {
			message.success("复制成功");
		}
	};
	const handleDeleteAlias = (value: string) => {
		console.log("handleDeleteAlias", value);
	};

	const handleAddAlias = (value: string) => {
		console.log("handleAddAlias", value);
		updateIndexInfoStatus.value = updateIndexInfoStatus.value + 1;
	};

	//   handleCloseIndex,
	//   handleOpenIndex,
	//   handleDeleteIndex,

	const handleOpenIndex = (value: string) => {
		return new Promise((resolve) => {
			console.log("handleOpenIndex", value);
			invoke("action_open_indices", { indices: [value] })
				.then((data: any) => {
					notification.info({
						message: "打开索引结果",
						description: JSON.stringify(data, null, "\t"),
						placement: "bottomRight",
					});
					getCatIndices();
				})
				.finally(() => {
					resolve(true);
				});
		});
	};
	const handleDeleteIndex = (value: string) => {
		return new Promise((resolve) => {
			invoke("action_remove_indices", { indices: [value] })
				.then((data: any) => {
					notification.info({
						message: "删除索引结果",
						description: JSON.stringify(data, null, "\t"),
						placement: "bottomRight",
					});
					emit("deleteIndex", value);
				})
				.finally(() => {
					resolve(true);
				});
		});
	};

	const handleCloseIndex = (value: string) => {
		return new Promise((resolve) => {
			invoke("action_close_indices", { indices: [value] })
				.then((data: any) => {
					notification.info({
						message: "关闭索引结果",
						description: JSON.stringify(data, null, "\t"),
						placement: "bottomRight",
					});
					getCatIndices();
				})
				.finally(() => {
					resolve(true);
				});
		});
	};

	const getCatIndices = function () {
		if (esIndexInfo.value) {
			invoke("get_cat_indices", { index: esIndexInfo.value.index }).then((data: any[] | undefined) => {
				if (data && data.length) {
					esIndexInfo.value = data.find((item: any) => {
						return item.index == esIndexInfo.value?.index;
					});
				}
			});
		}
	};

	watch(esIndexInfoTemp, () => {
		esIndexInfo.value = esIndexInfoTemp.value;
	});

	onMounted(function () {
		if (esIndexInfoTemp.value) {
			esIndexInfo.value = esIndexInfoTemp.value;
		}
	});

	function checkboxChange() {
		emit("checkboxChange", esIndexInfo.value?.index, checked.value);
	}

	return {
		statusCircleInner,
		statusCircleFill,
		cardWidth,
		openDrawer,
		checked,
		esIndexInfo,
		updateIndexInfoStatus,
		showDrawer,
		handleDrawerClose,
		updateIndexInfo,
		handleCopy,
		handleDeleteAlias,
		handleAddAlias,
		handleCloseIndex,
		handleOpenIndex,
		handleDeleteIndex,
		checkboxChange,
	};
}
